import { redirect } from 'next/navigation'
import { use } from 'react'

import { AuthLayout } from '@/components/layouts/AuthLayout'
import { createClient } from '@/utils/supabase/server'

export default function ProtectedLayout({
  children,
}: {
  children: React.ReactNode
}) {
  const supabase = use(createClient())
  const { data, error } = use(supabase.auth.getUser())

  if (error || !data.user) {
    redirect('/login')
  }

  return <AuthLayout user={data.user}>{children}</AuthLayout>
}
